/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WekaModels;

import grex.Data.ArffTableModel;
import grex.ErrorManager;
import grex.Prediction;
import grex.PredictionContainer;
import grex.IPredictiveModel;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.classifiers.trees.M5P;
import weka.core.Instance;
import weka.core.Instances;
import wekaGrexBridge.M5PReg;
import wekaGrexBridge.WekaArffTableModel;

/**
 *
 * @author RIK
 */
public class GrexM5P extends WekaPredictiveModel {

    private M5P m5p;

    public GrexM5P(ArffTableModel data) {
        super(data, new M5P());
        m5p = (M5P) model;
        m5p.setUseUnsmoothed(false);
        m5p.setBuildRegressionTree(false);
    }

    public String getName() {
        return "M5P";
    }

    public double getNrOfNodes() {
        try {
            return calcNumberOfNodes(m5p.graph());
        } catch (Exception ex) {
            ErrorManager.getInstance().reportError(ex);
        }
        return -999;
    }

    @Override
    public void execute(PredictionContainer pc) {
        for (Prediction p : pc.values()) {

            try {
                Instance instance = wekaArffTableModel.getInstance(p.getInstance(), wekaTrain);//wekaTrain is just used to set the Dataset in the instance
                double prediction;
                prediction = Math.max(model.classifyInstance(instance), 0);

                p.setProbs(model.distributionForInstance(instance));
                p.setPrediction(prediction);

            } catch (Exception ex) {
                ErrorManager.getInstance().reportError(ex);
            }

        }
    }

    @Override
    public String toString() {
        try {
            return m5p.graph();
        } catch (Exception ex) {
            return "ERROR";
        }
    }
}
